package 第二次实验.第四个实验;

import chapter10.jsjf.BinaryTreeNode;
import chapter10.jsjf.LinkedBinaryTree;

import java.util.ArrayList;

public class infixtree extends LinkedBinaryTree implements Comparable {
   public BinaryTreeNode node;
   public BinaryTreeNode left,right;
   public infixtree(){

   }

//   public BinaryTreeNode maketree(String Line) {
//       ArrayList kuohao = new ArrayList();
//       ArrayList zifu = new ArrayList();
//       ArrayList num = new ArrayList();
//
//       for (int a = 0; a < Line.length(); a++) {
//            if (Line.charAt(a)=='(') {
//                String temp = "";
//                for (int i = 0; i < Line.length(); i++) {
//                    if (Line.charAt(i) == '(') {
//                        while (Line.charAt(i + 1) != ')') {
//                            temp += Line.charAt(i + 1);
//                            i++;
//                        }
//                        break;
//                    }
//                }
//                kuohao.add(temp);
//
//            }
//           if (Line.charAt(a)=='+'||Line.charAt(a)=='-'){
//               zifu.add(Line.charAt(a));
//           }
//           else if (Line.charAt(a)=='*'||Line.charAt(a)=='/'){
//
//            }
//
//       }
//       return null;
//   }
    public BinaryTreeNode maketree(String Line){
        ArrayList<String > fuhao = new ArrayList<>();
        ArrayList<LinkedBinaryTree> num = new ArrayList<>();
        String a[] = Line.split(" ");

        for (int i =0;i<a.length;i++){
            if (a[i].equals("(")){
                String temp1 = "";
                while (true) {
                    if (!a[i+1].equals(")")){
                        temp1 += a[i+1] + " ";
                        }
                     else {
                        break;
                    }
                    i++;
                }
                chapter10.jsjf.LinkedBinaryTree temp = new chapter10.jsjf.LinkedBinaryTree();
                temp.root = maketree(temp1);
                num.add(temp);
                i++;
            }
            if (a[i].equals("+")||a[i].equals("-")){
                fuhao.add(String.valueOf(a[i]));
                }
                else if (a[i].equals("*")||a[i].equals("/")){
                LinkedBinaryTree left=num.remove(num.size()-1);
                String temp2=String.valueOf(a[i]);
                if (!a[i+1].equals("(")){
                    LinkedBinaryTree right = new LinkedBinaryTree(String.valueOf(a[i+1]));
                    LinkedBinaryTree node = new LinkedBinaryTree(temp2, left, right);
                    num.add(node);
                }
                else {
                    String temp3 = "";
                    while (true) {
                        if (!a[i+1].equals(")")){
                            temp3+=String.valueOf(a[i+1]);
                            }
                        else {
                            break;
                        }
                        i++;
                  }
                    LinkedBinaryTree temp4 = new LinkedBinaryTree();
                    temp4.root = maketree(temp3);
                    LinkedBinaryTree node1 = new LinkedBinaryTree(temp2, left, temp4);
                    num.add(node1);
                }
                }else {
            num.add(new LinkedBinaryTree(a[i]));}
        }
        while (fuhao.size() > 0) {
            LinkedBinaryTree left = num.remove(0);
            LinkedBinaryTree right = num.remove(0);
            String oper = fuhao.remove(0);

            LinkedBinaryTree node2 = new LinkedBinaryTree(oper, left, right);
            num.add(0, node2);
        }
        node = (num.get(0)).root;

        return node;
    }



    @Override
    public int compareTo(Object o) {
        if (o=="*"||o=="/"){
            return 1;
        }
        else {
            return -1;
        }

    }
}